<?php
	session_start();
	require_once('common/config.php');
	require_once('../_helper/paging.php');
	require_once('../_helper/helper.php');
	require_once('../_model/news_model.php');
	
	// if(isset($_GET['type']) && $_GET['type']== 'video')
		// $pageCMS = PAGE_CMS_VIDEO;
	
	if(isset($_GET['type']) && $_GET['type']== 'doc')
		$pageCMS = PAGE_CMS_DOC;
	
	if(!isset($_GET['type']))
		$pageCMS = PAGE_CMS_NEWS;
	
	$keyword = '';
	
	if(!isset($_SESSION['logon']))
	{
		header("Location: /wks/login.php?url=news");
		exit();
	}
	
	if (!isset($_GET['action']))
		$_GET['action'] = "list";
	
	if (isset($_GET['id'])){
		$news_id = $_GET['id'];
	}
	if (isset($_GET['keyword'])){
		$keyword = $_GET['keyword'];
	}
	
	$action = $_GET['action'];
	
	
		
	if($action == 'list' || $action =='search')
	{
		if(isset($_GET['page']))
		$curPage = $_GET['page'];
		else
		{
			$curPage = 0;
			unset($_SESSION['numNews']);
		}
		
		
		/*var_dump($type_arr);die();*/
		if( !isset($_SESSION['numNews']))
		{
			if($pageCMS == PAGE_CMS_NEWS)
				$numNews = GetNumNewsSearch($keyword);
			
			// if($pageCMS == PAGE_CMS_VIDEO)
				// $numNews = GetNumNewsSearch($keyword, NEWS_STATUS_VIDEO);	
				
			if($pageCMS == PAGE_CMS_DOC)
				$numNews = GetNumNewsSearch($keyword, NEWS_STATUS_DOC);	
				
			$_SESSION['numNews'] = $numNews;
		}
		else
		$numNews = $_SESSION['numNews'];
		
		if($pageCMS == PAGE_CMS_NEWS)
		{
			$param = "action=search&keyword={$keyword}";	
			$news_arr = GetNewsSearch($curPage, PAGING_NUM_ROW,$keyword);
		}
			
		
		// if($pageCMS == PAGE_CMS_VIDEO)
		// {
			// $param = "action=search&type=video&keyword={$keyword}";	
			// $news_arr = GetNewsSearch($curPage, PAGING_NUM_ROW,$keyword, NEWS_STATUS_VIDEO);
		// }
			
			
		if($pageCMS == PAGE_CMS_DOC)
		{
			$param = "action=search&type=doc&keyword={$keyword}";	
			$news_arr = GetNewsSearch($curPage, PAGING_NUM_ROW,$keyword, NEWS_STATUS_DOC);
		}
			
		//Xu ly
		require_once('template/news/news_list.php');
		exit();
	}																											
	
	if($action == 'add')
	{
		if(!isset($_POST['submit']))
		{
			require_once('template/news/news_add.php');
			exit();
		}
		else
		{
			if($_POST['news_title'] != '')
			{
				
				$order   = array("\r\n", "\n", "\r", "\t", "  ");
				$_POST['news_description'] 	= str_replace($order,'',$_POST['news_description']);
				$_POST['news_content'] 		= str_replace($order,'',$_POST['news_content']);
				
				if($pageCMS == PAGE_CMS_NEWS)
					$result = InsertNews($_POST);
				
				// if($pageCMS == PAGE_CMS_VIDEO)
					// $result = InsertNews($_POST, NEWS_STATUS_VIDEO);
					
				if($pageCMS == PAGE_CMS_DOC)
					$result = InsertNews($_POST, NEWS_STATUS_DOC);
					
				if($result)
				{
					if($pageCMS == PAGE_CMS_NEWS)
						header("Location: /wks/news.php?action=list");
						
					// if($pageCMS == PAGE_CMS_VIDEO)
						// header("Location: /wks/news.php?action=list&type=video");
						
					if($pageCMS == PAGE_CMS_DOC)
						header("Location: /wks/news.php?action=list&type=doc");
				}
				else
				header("Location: /wks/news.php?action=list&error=1&e_action=add");
			}
			else
			header("Location: /wks/news.php?action=list&error=1&e_action=add");
		}
		exit();
	}																												
	
	if($action == 'edit')
	{
		if (!isset($news_id))
		header("Location: /wks/news.php");		
		
		if(!isset($_POST['submit'])){
			$news_value = GetNewsById($news_id);
			require_once('template/news/news_edit.php');
			exit();
		}
		else{
			if($_POST['news_title'])
			{
				$order   = array("\r\n", "\n", "\r", "\t", "  ");
				$_POST['news_description'] 	= str_replace($order,'',$_POST['news_description']);
				$_POST['news_content'] 		= str_replace($order,'',$_POST['news_content']);
				
				UpdateNews($news_id, $_POST);
				if($pageCMS == PAGE_CMS_NEWS)
					header("Location: /wks/news.php?action=list");
				// if($pageCMS == PAGE_CMS_VIDEO)
					// header("Location: /wks/news.php?type=video&action=list");
				if($pageCMS == PAGE_CMS_DOC)
					header("Location: /wks/news.php?type=doc&action=list");
			}
		}
		exit();
	}																										
	
	if($action == 'del')
	{
		$result = FALSE;
		if (isset($news_id))
		$result = DeleteNews($news_id);
		
		if($result == FALSE)
		{
			if($pageCMS == PAGE_CMS_NEWS)
				header("Location: /wks/news.php?error=1&e_action=del");	
				
			// if($pageCMS == PAGE_CMS_VIDEO)
				// header("Location: /wks/news.php?type=video&error=1&e_action=del");	
				
			if($pageCMS == PAGE_CMS_DOC)
				header("Location: /wks/news.php?type=doc&error=1&e_action=del");	
		}
		else
		{
			if($pageCMS == PAGE_CMS_NEWS)
				header("Location: /wks/news.php");	
				
			// if($pageCMS == PAGE_CMS_VIDEO)
				// header("Location: /wks/news.php?type=video");	
			
			if($pageCMS == PAGE_CMS_DOC)
				header("Location: /wks/news.php?type=doc");	
		}
	}											
	if($action == 'adjorder')
	{
		//var_dump($_GET); die();
		$news_id = $_GET['id'];
		
		if($_GET['adj'] == 'inc')
		{
			if (UpdateNewsOrder($news_id, '+') )
			echo "ok";
			else
			echo "fail";
		}
		if($_GET['adj'] == 'desc')
		{
			if (UpdateNewsOrder($news_id, '-') )
			echo "ok";
			else
			echo "fail";
		}
		exit();
	}																			
	if($action == 'adjnew')
	{
		$news_id = $_GET['id'];
		
		if($_GET['adj'] == 'new')
		{
			if (UpdateNewsNew($news_id, 'new') )
				echo "ok";
			else
				echo "fail";
		}
		if($_GET['adj'] == 'old')
		{
			if (UpdateNewsNew($news_id, 'old') )
				echo "ok";
			else
				echo "fail";
		}
		exit();
	}																		
	
?>